﻿using ConfExpert.Utility;
using DevExpress.XtraReports.UI;
using DevTeam;
using System;
using System.Data;
using System.Windows.Forms;

namespace ConfExpert.Forms.Depo
{
    public partial class AksIrsaliyeOlustur : XPopupForm
    {
        public AksIrsaliyeOlustur()
        {
            InitializeComponent();
        }
        public AksIrsaliyeOlustur(int _Kaynak, int _Cari, string _CikisPartisi, int kesilenIrsaliye)
            : this()
        {
            Kaynak = _Kaynak;
            Cari = _Cari;
            CikisPartileri = _CikisPartisi;
            KesilenIrsaliye = kesilenIrsaliye;
        }

        string CikisPartileri;
        int Kaynak, Cari, nextIrsaliye, KesilenIrsaliye;
        private void AksIrsaliyeOlustur_Load(object sender, EventArgs e)
        {
            var irsaliyeNo = intParse(ExecuteScalar("getMaxIrsaliyeNumber2", MyDept, cbTip.SelectedIndex));

            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(irsaliyeNo) + 1);
            gleSevkiyatci.Properties.Assign(RI(Rp.Sevkiyatci));
            gleFirm.Properties.Assign(RI(Rp.Cari));

            dateIrsaliye.EditValue = DateTime.Now;
            dateSevk.EditValue = DateTime.Now;

            btnOk.Click += btnOk_Click;
            btnCancel.Click += btnCancel_Click;
            cbTip.SelectedIndexChanged += cbTip_SelectedIndexChanged;

            gleKaynak.Properties.Assign(GetGlKaynak());
            gleKaynak.EditValueChanged += gleKaynak_EditValueChanged;
            gleKaynak.Properties.ReadOnly = true;
            gleKaynak.EditValue = Kaynak;
            gleFirm.EditValue = Cari;
        }

        void gleKaynak_EditValueChanged(object sender, EventArgs e)
        {
            gleFirm.Enabled = true;
            gleFirm.EditValue = null;

            switch (intParse(gleKaynak.EditValue))
            {
                case 1: //Tedarikçi
                    gleFirm.Properties.Assign(RI(Rp.V_CariV));
                    break;
                case 2: //Müşteri
                    gleFirm.Properties.Assign(RI(Rp.V_CariC));
                    break;
                case 4: //Atölye
                    gleFirm.Properties.Assign(RI(Rp.V_CariA));
                    break;
            }
        }


        void cbTip_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable dt = ExecuteDataTable("getMaxIrsaliyeNumber2", MyDept, cbTip.SelectedIndex);
            DataRow dr = dt.Rows[0];
            txtIrsaliyeNo.Text = String.Format("{0:000000}", intParse(dr["IrsaliyeNumber"]) + 1);
        }

        void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        void btnOk_Click(object sender, EventArgs e)
        {
            nextIrsaliye = (Convert.ToInt32(txtIrsaliyeNo.Text)) - 1;
            this.DialogResult = DialogResult.OK;

            string TeslimEden;
            if (gleSevkiyatci.EditValue == null)
                TeslimEden = "";
            else
                TeslimEden = AppSession.Lookups.Sevkiyatci.FindBySevkiyatciId(intParse(gleSevkiyatci.EditValue)).Name;

            Rapor.AksesuarIrsaliye irs = new Rapor.AksesuarIrsaliye(CikisPartileri, txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, TeslimEden, txtTeslimAlan.Text);

            irs.PrinterName = "OKI Generic Dot-Matrix 9Pin Printer";
            ReportPrintTool printTool = new ReportPrintTool(irs);
            irs.PrintingSystem.StartPrint += PrintingSystem_StartPrint;
            irs.PrintingSystem.EndPrint += PrintingSystem_EndPrint;
            irs.PrintProgress += irs_PrintProgress;
            irs.CreateDocument();
            irs.ShowPrintMarginsWarning = false;
            irs.ShowPrintStatusDialog = false;
            irs.ShowPreviewDialog();
        }

        void PrintingSystem_EndPrint(object sender, EventArgs e)
        {
            ExecuteNonQuery("UpdPartiSevkiyatci", CikisPartileri, gleSevkiyatci.EditValue);
        }

        void PrintingSystem_StartPrint(object sender, DevExpress.XtraPrinting.PrintDocumentEventArgs e)
        {
            e.PrintDocument.PrinterSettings.Copies = 1;
            e.PrintDocument.PrinterSettings.Collate = false;
        }

        void irs_PrintProgress(object sender, DevExpress.XtraPrinting.PrintProgressEventArgs e)
        {
            if (KesilenIrsaliye > 0)
            {
                Session.DataManager.ExecuteDataSet("RemoveIrsaliyeNumber", CikisPartileri);
                KesilenIrsaliye = 0;
            }
            nextIrsaliye++;
            Session.DataManager.ExecuteDataSet("createIrsaliye2", CikisPartileri, txtIrsaliyeNo.Text, dateSevk.DateTime, txtNot.Text, nextIrsaliye, MyDept, cbTip.SelectedIndex);
        }

    }
}